package Demo2;

import Demo3.SingleList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DELL
 * Date: 2023-04-20
 * Time: 10:27
 */
public class Run {


    // 合并两个有序链表
    public static SingleList.ListNode mergeTwoLists(SingleList.ListNode head1, SingleList.ListNode head2) {
        if(head1 == null) {
            return head2;
        }
        if(head2 == null) {
            return head1;
        }

        // 定义虚拟节点
        SingleList.ListNode newHead = new SingleList.ListNode(-1);
        SingleList.ListNode tmp = newHead;
        while(head1 != null && head2 != null) {
            // 定义临时节点记录比较后的最小节点位置
            if(head1.val < head2.val) {
                tmp.next = head1;
                head1 = head1.next;
            }else {
                tmp.next = head2;
                head2 = head2.next;
            }
            tmp = tmp.next;
        }

        // 循环结束后, 此时 head1 为null  或者 head2 为null
        if(head1 != null) {
            // head2 为null, 此时说明 head1 后面的节点都比前面的节点大
            tmp.next = head1;
        }
        if(head2 != null) {
            tmp.next = head2;
        }

        // 返回虚拟节点的next
        return newHead.next;
    }




    public static void main(String[] args) {
        // 需要注意的时, 尾插才能正确升序排序!!!
        SingleList singleList = new SingleList();
        singleList.tailAdd(11);
        singleList.tailAdd(21);
        singleList.tailAdd(32);
        singleList.tailAdd(40);

        SingleList singleList1 = new SingleList();
        singleList1.tailAdd(8);
        singleList1.tailAdd(15);
        singleList1.tailAdd(30);
        singleList1.tailAdd(66);
        singleList1.tailAdd(100);

        SingleList.ListNode newHead = mergeTwoLists(singleList.head, singleList1.head);
        singleList.headDisPlay(newHead);
    }

    public static void main2(String[] args) {
        SingleList singleList = new SingleList();
        singleList.tailAdd(1);
        singleList.tailAdd(2);
        singleList.tailAdd(3);
        singleList.tailAdd(4);
        singleList.tailAdd(5);
        singleList.headDisPlay(singleList.reserve());
    }


    public static void main1(String[] args) {
//        MySingleList mySingleList = new MySingleList();
//        mySingleList.displayNode();
//        System.out.println("====");
//        System.out.println(mySingleList.size());
//        System.out.println(mySingleList.contains(11));
//        System.out.println(mySingleList.contains(10));
//        System.out.println("====");
////        mySingleList.headAdd(11);
////        mySingleList.headAdd(22);
////        mySingleList.headAdd(33);
////        mySingleList.headAdd(44);
////        mySingleList.headAdd(55);
//        mySingleList.tailAdd(11);
//        mySingleList.tailAdd(22);
//        mySingleList.tailAdd(33);
//        mySingleList.tailAdd(44);
//        mySingleList.tailAdd(55);
//
//        System.out.println(mySingleList.reserve());
//        mySingleList.displayNode();
    }

}
